package com.tongbo.mobilesafe.ui;

import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.adpater.MainUIAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
	private static final String TAG = "MainActivity";
	private GridView gv_main;
	private MainUIAdapter adapter;
	// 用来持久化一些配置信息
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainscreen);
		// 初始化sharedpreferences
		sp = this.getSharedPreferences("config", MODE_PRIVATE);
		gv_main = (GridView) this.findViewById(R.id.gv_main);
		adapter = new MainUIAdapter(this);
		Log.i(TAG, (null == gv_main) + "");
		gv_main.setAdapter(adapter);
		gv_main.setOnItemClickListener(this);
		gv_main.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, final View view,
					int position, long id) {
				if (position == 0) {
					AlertDialog.Builder builder = new Builder(MainActivity.this);
					builder.setTitle("设置");
					builder.setMessage("请输入要更改的名称");
					final EditText et = new EditText(MainActivity.this);
					et.setHint("请输入文本");
					builder.setView(et);
					builder.setPositiveButton("确定", new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String name = et.getText().toString().trim();
							if ("".equals(name)) {
								Toast.makeText(getApplicationContext(),"内容不能为空", 0).show();
								return;
							} else {
								Editor editor = sp.edit();
								editor.putString("lost_name", name);
								editor.commit();// 完成数据的提交
								TextView tv = (TextView) view.findViewById(R.id.tv_main_name);
								tv.setText(name);
							}
						}
					});
					builder.setNegativeButton("取消", new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							
						}
					});
					builder.create().show();
				}
				return false;
			}
		});
	}
	/**
	 * 当gridview的条目被点击的时候 对应的回调 parent :　girdview view : 当前被点击的条目 Linearlayout
	 * position : 点击条目对应的位置 id : 代表的行号
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		Log.i(TAG, "点击的位置"+position);
		switch (position) {
		case 0://手机防盗
			Log.i(TAG, "进入手机防盗");
			Intent lostIntent = new Intent(MainActivity.this,LostProtectedActivity.class);
			startActivity(lostIntent);
			break;
		case 1://通讯卫士
			Log.i(TAG, "进入通讯卫士");
			Intent callSmsIntent = new Intent(MainActivity.this,CallSMSActivity.class);
			startActivity(callSmsIntent);
			break;
		case 7://高级工具
			Log.i(TAG, "进入高级工具");
			Intent atoolsIntent = new Intent(MainActivity.this,AToolsActivity.class);
			startActivity(atoolsIntent);
			break;
		}
	}
}
